﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.Diagnostics;

namespace SuperDeepZoom
{
    /// <summary>
    /// Helper class to encapsulate row and column behavior for MultiScaleSubImage
    /// This class is used during image arrangement in ImageArranger class.
    /// It has minimal functionality (i.e. not much to see here).
    /// </summary>
    internal class SubImage
    {

        private Size _imageSize = new Size(0.0, 1.0);	//normalized height is 1 for all images
        private int _colNum = 0;
        private int _rowNum = 0;
        MultiScaleSubImage image;

        internal SubImage(MultiScaleSubImage image)
        {
            this.image = image;
            // Capture the normalized size of each image (fixed height of 1)
            // This normalization is required since we want the height of all images to be the same but the widths can differ
            _imageSize.Width = image.AspectRatio;
        }

        public MultiScaleSubImage Image
        {
            get
            {
                return image;
            }
        }


        internal int ColNum
        {
            get { return _colNum; }
            set { _colNum = value; }
        }

        internal int RowNum
        {
            get { return _rowNum; }
            set { _rowNum = value; }
        }

        internal double Width { get { return _imageSize.Width; } }
        internal double Height { get { return _imageSize.Height; } }

        internal void Scale(double scaleBy)
        {
            _imageSize.Width *= scaleBy;
            _imageSize.Height *= scaleBy;
        }
    }

    /// <summary>
    /// Encapsulate the notion of a row
    /// </summary>
    internal class Row
    {
        List<SubImage> _subImages = new List<SubImage>();
        double _spaceBetweenImages = 0;

        internal Row(double spaceBetweenImage)
        {
            _spaceBetweenImages = spaceBetweenImage;
        }

        internal double TotalWidth
        {
            get
            {
                double totalWidth = 0;
                _subImages.ForEach(image => totalWidth += image.Width);
                return totalWidth;
            }
        }

        internal int ImageCount { get { return _subImages.Count; } }
        internal double Height { get { return (ImageCount <= 0) ? 0 : _subImages[0].Height; } }
        internal double TotalSpaceBetweenImages { get { return (ImageCount <= 0) ? 0 : (ImageCount - 1) * _spaceBetweenImages; } }

        internal void AddImage(SubImage subImage)
        {
            _subImages.Add(subImage);
            subImage.ColNum = _subImages.Count - 1;
        }

        internal void Scale(double canvasWidth)
        {
            double scaleBy = (canvasWidth - TotalSpaceBetweenImages) / TotalWidth;
            foreach (SubImage subImage in _subImages)
                subImage.Scale(scaleBy);
        }

        internal double CalcX(int colNum)
        {
            Debug.Assert(colNum < _subImages.Count);

            double X = 0;
            for (int i = 0; i < colNum; i++)
            {
                X += _subImages[i].Width + _spaceBetweenImages;
            }
            return X;
        }
    }
}
